////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//   Software       :                                                         //
//   Layer          : SRC                                                     //
//   Package        :                                                         //
//   Component      :                                                         //
//                                                                            //
//   File           : main.cxx                                                //
//   Author         : RSO ( Rafael Sotomayor Brûlé )                          //
//   EMail          : rsotomayor@savtec.cl                                    //
//   Type           : C++ Source                                              //
//   Usage          :                                                         //
//   Purpose        : Programa Principal                                      //
//                                                                            //
//                                                                            //
//   who when        what                                                     //
//   ~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//   RSO 09-10-2010  Creation                                                 //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>

#include <assert.h>
#include <fcntl.h>
#include <sys/mman.h>

// #include <glib/gthread.h>

#include <gtk/gtk.h>
#include <glade/glade.h>

#include "mylib/errlog.h"
#include "mylib/utilconf.h"

#include "formmain.h"
#include "formconfig.h"
#include "myconfig.h"
#include "myglobal.h"
#include "cdatabase.h"
#include "myutil.h"

FormMain    *myFormMain_g;
FormConfig  *myFormConfig_g;
MyConfig    *config_g;
MyGlobal    *global_g;
CDataBase   *database_g;
errLog       log_g;
MyUtil      *myUtil_g;


int main (int argc_p, char *argv_p[]) {
  global_g = new MyGlobal();

  signal(SIGTERM,CGlobal::myhandleSignal);
  signal(SIGINT,CGlobal::myhandleSignal);
  signal(SIGFPE,CGlobal::myhandleSignal);
  signal(SIGSEGV,CGlobal::myhandleSignal);

  global_g->programName_a = basename(argv_p[0]);

  if ( argc_p < 2 ) {
    fprintf(stderr,(char *)"Usage: %s <FileConfig>\n",basename(argv_p[0]));
    exit(EXIT_FAILURE);
  }   

  struct stat buffer;
  int    status ;

  status = stat(argv_p[1], &buffer);
  if ( status == -1 ) {
	  fprintf(stderr,"Usage: %s <FileConfig.db>\n",basename(argv_p[0]));
	  fprintf(stderr,"Error: No puede leer %s\n",argv_p[1]);
	  exit(EXIT_FAILURE);
  }

//  setlocale (LC_NUMERIC,  "en_US.UTF-8");
//  setlocale (LC_MONETARY, "en_US.UTF-8");
//  setlocale (LC_TIME,     "en_US.UTF-8");


  global_g->myconfigFile_a = argv_p[1];

  config_g = new MyConfig();
  config_g->init(global_g->myconfigFile_a);  
  config_g->readParametros();

  myUtil_g = new MyUtil();
  myUtil_g->checkTemperature();
  myUtil_g->setTimeUp();

  if ( config_g->geoenabled_a ) {
    global_g->startGeoServicio();
  } else {
    global_g->readGeoData();
  }

  global_g->startTareas();

  log_g.SETSCOPE(global_g->programName_a.data());
  log_g.SetLogLevel((errLog::eLogSeverity) config_g->loglevel_a);
  log_g.Enable();

  printf("myconfigFile_g [%s]\n",global_g->myconfigFile_a.data());
  printf("myconfigFile_g [%s]\n",global_g->myconfigFile_a.data());

  errLog::LOGERR((errLog::INFO,"myconfigFile_g [%s]\n",global_g->myconfigFile_a.data()));
  errLog::LOGERR((errLog::INFO,"LogLevel [%d]\n",config_g->loglevel_a));
  errLog::LOGERR((errLog::INFO,"Path Home [%s]\n",config_g->pathHome_a.data()));
  errLog::LOGERR((errLog::INFO,"DB Maestro [%s]\n",config_g->dbmaestros_a.data()));
  errLog::LOGERR((errLog::INFO,"Image Path [%s]\n",config_g->pathImage_a.data()));

  database_g = new CDataBase();
  database_g->initMaestros(config_g->dbmaestros_a);

#if defined(__arm__)
 // Secure glib 
  if( ! g_thread_supported() ) {
    g_thread_init( NULL );
  }
#endif

  // Secure gtk
  gdk_threads_init();
  gtk_set_locale ();
  gtk_init (&argc_p, &argv_p);


  string   gladeFile;
  string   dummy ;
  if ( config_g->getParametro("gladefile","global",dummy) == 0 ) {
    gladeFile = dummy;
  } else {
    gladeFile = "s100-fp.ui" ;
  }

  errLog::LOGERR((errLog::TRACE,"Glade File [%s]\n",gladeFile.data()));

  myFormMain_g = new FormMain();
  myFormMain_g->initializeBuilder(gladeFile);
  myFormMain_g->setCallBack();
  myFormMain_g->show();
  myFormMain_g->initServicios();

  
	// Obtain gtk's global lock
  gdk_threads_enter();  
  gtk_main ();
  gdk_threads_leave();

  errLog::LOGERR((errLog::TRACE,"gdk_threads_leave\n"));

  delete myFormMain_g;

  delete database_g;
  delete global_g;
  delete config_g;
  delete myUtil_g;

  return 0;
}




